
package com.neusoft.neuiotms.dcom.repository.impl;

import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import com.neusoft.bizcore.web.jpa.repository.AbstractRepository;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcom.model.DutyArrangement;
import com.neusoft.neuiotms.dcom.model.QDutyArrangement;
import com.neusoft.neuiotms.dcom.repository.CustomizedDutyArrangementRepository;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.jpa.impl.JPAQuery;

public class CustomizedDutyArrangementRepositoryImpl extends AbstractRepository
        implements CustomizedDutyArrangementRepository {

    @Override
    public List<DutyArrangement> index(final Searchable searchable) {
        final QDutyArrangement qDutyArrangement = QDutyArrangement.dutyArrangement;
        final BooleanBuilder where = this.buildWhere(searchable);
        final JPAQuery<DutyArrangement> query = this.query().selectFrom(qDutyArrangement);
        query.where(where);
        return query.fetch();
    }

    @Override
    public Page<DutyArrangement> search(final Pageable pageable, final Searchable searchable) {
        final QDutyArrangement qDutyArrangement = QDutyArrangement.dutyArrangement;
        final BooleanBuilder where = this.buildWhere(searchable);
        final JPAQuery<DutyArrangement> query = this.query().selectFrom(qDutyArrangement);
        query.where(where);
        return super.search(query, pageable);
    }

    @Override
    protected Class<?> getModelClass() {
        return DutyArrangement.class;
    }

    private BooleanBuilder buildWhere(final Searchable searchable) {
        final BooleanBuilder where = new BooleanBuilder();
        final QDutyArrangement qDutyArrangement = QDutyArrangement.dutyArrangement;

        // 名称检索
        where.and(super.containsStr(qDutyArrangement.name, searchable, "name"));
        // 组织机构检索
        where.and(super.equalsStr(qDutyArrangement.orgCode, searchable, "orgCode"));
        // 删除状态
        where.and(super.equalsBool(qDutyArrangement.deleteFlag, searchable, "deleteFlag"));
        // 组织权限
        where.and(super.inStrings(qDutyArrangement.orgCode, searchable, "orgCodes"));

        return where;
    }
}
